#include "stm32f10x.h" // Device header
#include "UsePins.h"
#include "SignalLight.h"
#include "Delay.h"
void Light_Run()
{
    int i;
    for (i = 0; i < 10; i++)
    {
        if (i % 3 == 0)
        {
            GPIO_SetBits(Light_GPIO, Lingt_Red_PIN);
            GPIO_ResetBits(Light_GPIO, Lingt_Green_PIN);
            GPIO_ResetBits(Light_GPIO, Lingt_Yellow_PIN);
            Delay_ms(300);
        }
        else if (i % 3 == 1)
        {
            GPIO_ResetBits(Light_GPIO, Lingt_Red_PIN);
            GPIO_SetBits(Light_GPIO, Lingt_Green_PIN);
            GPIO_ResetBits(Light_GPIO, Lingt_Yellow_PIN);
            Delay_ms(300);
        }
        else
        {
            GPIO_ResetBits(Light_GPIO, Lingt_Red_PIN);
            GPIO_ResetBits(Light_GPIO, Lingt_Green_PIN);
            GPIO_SetBits(Light_GPIO, Lingt_Yellow_PIN);
            Delay_ms(300);
        }
    }

    GPIO_ResetBits(Light_GPIO, Lingt_Red_PIN);
    GPIO_ResetBits(Light_GPIO, Lingt_Green_PIN);
    GPIO_SetBits(Light_GPIO, Lingt_Yellow_PIN);
}

void DisableFanPin()
{
    GPIO_InitTypeDef initStruct;
    TIM_TimeBaseInitTypeDef timBaseDef;
    TIM_OCInitTypeDef ocTypeDef;
    GPIO_InitTypeDef initStructureGreen;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    initStructureGreen.GPIO_Pin = Fan_Pin;
    initStructureGreen.GPIO_Speed = GPIO_Speed_50MHz;
    initStructureGreen.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_Init(GPIOB, &initStructureGreen);
    GPIO_WriteBit(GPIOB, GPIO_Pin_8, 1);
}

void Light_Init(void)
{
    GPIO_InitTypeDef initStruct;
    TIM_TimeBaseInitTypeDef timBaseDef;
    TIM_OCInitTypeDef ocTypeDef;
    GPIO_InitTypeDef initStructureGreen;

    RCC_APB2PeriphClockCmd(Light_RCC_GPIOX | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);

    initStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    initStruct.GPIO_Pin = Lingt_Red_PIN | Lingt_Green_PIN | Lingt_Yellow_PIN;
    initStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(Light_GPIO, &initStruct);

    Light_Run();
}

void Green_Light_Init()
{

    GPIO_InitTypeDef initStruct;
    TIM_TimeBaseInitTypeDef timBaseDef;
    TIM_OCInitTypeDef ocTypeDef;
    GPIO_InitTypeDef initStructureGreen;

    initStructureGreen.GPIO_Mode = GPIO_Mode_AF_PP;
    initStructureGreen.GPIO_Pin = Lingt_Green_PIN;
    initStructureGreen.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(Light_GPIO, &initStructureGreen);

    timBaseDef.TIM_Period = 100;
    timBaseDef.TIM_Prescaler = 720;
    timBaseDef.TIM_CounterMode = TIM_CounterMode_Up;
    timBaseDef.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM4, &timBaseDef);

    ocTypeDef.TIM_OCMode = TIM_OCMode_PWM1;
    ocTypeDef.TIM_OCNPolarity = TIM_OCPolarity_High;
    ocTypeDef.TIM_OutputState = TIM_OutputState_Enable;
    ocTypeDef.TIM_Pulse = 50;

    TIM_OC2Init(TIM4, &ocTypeDef);
    TIM_Cmd(TIM4, ENABLE);

    DisableFanPin();
}

void Light_PWM_SetCompare(uint16_t compare)
{
    TIM_SetCompare2(TIM4, compare);
}

void LightWarning(uint8_t i)
{
    GPIO_ResetBits(Light_GPIO, Lingt_Green_PIN);

    if (i % 2 == 0)
    {
        Delay_ms(200);
        GPIO_ResetBits(Light_GPIO, Lingt_Red_PIN);
    }
    else
    {
        Delay_ms(200);
        GPIO_SetBits(Light_GPIO, Lingt_Red_PIN);
    }
}

void LightYellow(uint8_t x)
{
    GPIO_WriteBit(Light_GPIO, Lingt_Yellow_PIN, x);
}
